package jdbc;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class TestJdbc1 {
    public static void main(String[] args) throws SQLException {
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入sn: ");
        String sn=scanner.next();
        System.out.println("请输入name: ");
        String name=scanner.next();
        //1,创建数据源对象:描述了数据从哪里来,服务器在哪
        DataSource dataSource =new MysqlDataSource();
        //URL称为"唯一资源定位符"用来描述一个网络上的资源位置
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/myjava?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("0304");
        /* MysqlDataSource mysqlDataSource=new MysqlDataSource();
        mysqlDataSource.setUrl();
        mysqlDataSource.setUser();
        mysqlDataSource.setPassword();*/
        //2,和数据库服务器建立连接
        Connection connection=dataSource.getConnection();
        //3,程序构造SQL语句
        String sql="insert into student(sn,name) values(?,?)";
        PreparedStatement statement=connection.prepareStatement(sql);
        statement.setString(1,sn);
        statement.setString(2,name);
        //4,把SQL语句发送到服务器中进行执行
        int n=statement.executeUpdate();
        System.out.println(n);
        //5,释放上述资源
        statement.close();
        connection.close();
    }
}
